import RadioGroupMgr from './RadioGroupMgr';
const {ccclass, property} = cc._decorator;

@ccclass
export default class RadioButton extends cc.Component{


    
    target: cc.Node = null;

    @property(cc.SpriteFrame)
    sprite: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    checkedSprite: cc.SpriteFrame = null;

    @property
    groupId = -1;

    @property
    checked = false;

    onLoad(){

        if(cc.vv == null){
            return;
        }

        if(cc.vv.radioGroupMgr == null){
            cc.vv.radioGroupMgr = new RadioGroupMgr();
            cc.vv.radioGroupMgr.init();
        }
        cc.vv.radioGroupMgr.add(this);
        this.target = this.node.getChildByName('button');
        cc.vv.utils.addClickEvent(this.target, this.node, 'RadioButton', 'onClicked');
        this.refresh();
    }

    check(value: boolean){
        this.checked = value;
        this.refresh();
    }


    refresh(){
        const targetSprite = this.target.getComponent(cc.Sprite);
        targetSprite.spriteFrame = this.checked ? this.checkedSprite : this.sprite;
    }

    onClicked(){
        cc.vv.radioGroupMgr.check(this);
    }

}